﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ResourceManager : MonoBehaviour
{
    public static ResourceManager Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = FindObjectOfType<ResourceManager>();
            }
            if (!_Instance)
            {
                GameObject obj = new GameObject("ResourceManager");
                _Instance = obj.AddComponent<ResourceManager>();
            }
            return _Instance;
        }
        set
        {
            _Instance = value;
        }
    }
    private static ResourceManager _Instance;

    [SerializeField]
    public Text uiText;

    //在编辑器内定义资源初始值
    [System.Serializable]
    public struct ResourceInitProperty
    {
        public ResourceType type;
        public float initCount;
        public float threshold;
    }

    [SerializeField]
    private List<ResourceInitProperty> initProperties = new List<ResourceInitProperty>();
    private Dictionary<ResourceType, float> resourceCurrCountDict = new Dictionary<ResourceType, float>();
    private Dictionary<ResourceType, float>  resourceThresholdDict = new Dictionary<ResourceType, float>();

    private void Start()
    {
        InitResourceProperties();
        if (ConstOfMap.loadgame)
            SetResource(ResourceType.Money, ConstOfMap.save.money);
        RefreshUI();
    }

    private void InitResourceProperties()
    {
        foreach (ResourceInitProperty p in initProperties)
        {
            if (p.type == ResourceType.None)
                continue;
            resourceCurrCountDict[p.type] = p.initCount;
            resourceThresholdDict[p.type] = p.threshold; 
        }
    }

    public bool TryConsumeResource(ResourceType resourceType, float count)
    {
        if(!resourceCurrCountDict.ContainsKey(resourceType))
        {
            Debug.LogError("Please set the Resource init Properties!");
            return false;
        }
        if(count > resourceCurrCountDict[resourceType])
        {
            return false;
        }
        else
        {
            resourceCurrCountDict[resourceType] -= count;
            RefreshUI();
            return true;
        }
    }

    private void SetResource(ResourceType resourceType, float count)
    {
        if (!resourceCurrCountDict.ContainsKey(resourceType) || !resourceThresholdDict.ContainsKey(resourceType))
        {
            Debug.LogError("Please set the Resource init Properties!");
            return;
        }
        if (count >= resourceThresholdDict[resourceType])
        {
            resourceCurrCountDict[resourceType] = resourceThresholdDict[resourceType];
        }
        else
        {
            resourceCurrCountDict[resourceType] = count;
        }
        RefreshUI();
    }

    public void GainResource(ResourceType resourceType, float count)
    {
        if (!resourceCurrCountDict.ContainsKey(resourceType) || !resourceThresholdDict.ContainsKey(resourceType))
        {
            Debug.LogError("Please set the Resource init Properties!");
            return;
        }
        if(resourceCurrCountDict[resourceType] + count >= resourceThresholdDict[resourceType])
        {
            resourceCurrCountDict[resourceType] = resourceThresholdDict[resourceType];
        }
        else
        {
            resourceCurrCountDict[resourceType] += count;
        }
        RefreshUI();
    }

    public bool TryConsumeResourceForBuilding(BuildingType type)
    {
        ResourceType resourceType = BuildingManager.Instance.GetRequiredResourceType(type);
        float count = BuildingManager.Instance.GetRequiredCost(type);
        return TryConsumeResource(resourceType, count);
    }

    public bool CheckResourceForBuilding(BuildingType type)
    {
        ResourceType resourceType = BuildingManager.Instance.GetRequiredResourceType(type);
        float count = BuildingManager.Instance.GetRequiredCost(type);
        if (!resourceCurrCountDict.ContainsKey(resourceType))
        {
            Debug.LogError("Please set the Resource init Properties!");
            return false;
        }
        if (count > resourceCurrCountDict[resourceType])
        {
            MessageController.Instance.SendMsg("金钱不足！");
            return false;
        }
        else
        {
            return true;
        }
    }

    public bool CheckResource(ResourceType resourceType, float count)
    {
        if (!resourceCurrCountDict.ContainsKey(resourceType))
        {
            Debug.LogError("Please set the Resource init Properties!");
            return false;
        }
        if (count > resourceCurrCountDict[resourceType])
        {
            MessageController.Instance.SendMsg("金钱不足！");
            return false;
        }
        else
        {
            return true;
        }
    }

    public float GetCurrResourceCount(ResourceType resourceType)
    {
        if(resourceCurrCountDict.ContainsKey(resourceType))
        {
            return resourceCurrCountDict[resourceType];
        }
        return 0;
    }

    void RefreshUI()
    {
        uiText.text = System.Math.Round(resourceCurrCountDict[ResourceType.Money]).ToString();
    }
}
